﻿using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Util
{
    /// <summary>
    /// 上传文件操作
    /// </summary>
    public static class UploadOperate
    {
        /// <summary>
        /// 多文件复刻上传
        /// </summary>
        /// <param name="fileCollection"></param>
        /// <param name="folder"></param>
        /// <returns></returns>
        public static List<string> UploadCopy(FormFileCollection fileCollection, string folder)
        {
            List<string> lst = new List<string>();
            foreach (IFormFile file in fileCollection)
            {
                string cb = CopyTo(file, folder);
                lst.Add(cb);
            }
            return lst;
        }

        /// <summary>
        /// 多文件流式上传
        /// </summary>
        /// <returns></returns>
        public static List<string> UploadSteam(FormFileCollection fileCollection, string folder)
        {
            List<string> lst = new List<string>();
            foreach (IFormFile file in fileCollection)
            {
                string cb = ReadSteam(file, folder);
                lst.Add(cb);
            }
            return lst;
        }

        /// <summary>
        /// 复刻缓存上传
        /// </summary>
        /// <param name="file">上传的文件</param>
        /// <param name="folder">目标文件夹</param>
        /// <returns>随机文件名</returns>
        public static string CopyTo(IFormFile file, string folder)
        {
            return CopyTo(file, folder, $"{Guid.NewGuid():N}");
        }

        /// <summary>
        /// 复刻缓存上传
        /// </summary>
        /// <param name="file">上传的文件</param>
        /// <param name="folder">目标文件夹</param>
        /// <param name="filename">不带后缀的文件名</param>
        /// <returns>指定文件名</returns>
        public static string CopyTo(IFormFile file, string folder, string filename)
        {
            string fname = file.FileName;
            string lastname = Path.GetExtension(fname);
            string extname = $"{filename}{lastname}";
            string filepath = $"{folder}{extname}";
            //验证并创建文件夹
            FileOperate.CreateFile(filepath);

            using FileStream fs = File.Create(filepath);
            // 复制文件
            file.CopyTo(fs);
            // 清空缓冲区数据
            fs.Flush();
            return extname;
        }

        /// <summary>
        /// 流上传
        /// </summary>
        /// <param name="file">上传的文件</param>
        /// <param name="folder">目标文件夹</param>
        /// <returns>随机文件名</returns>
        public static string ReadSteam(IFormFile file, string folder)
        {
            return ReadSteam(file, folder, $"{Guid.NewGuid():N}");
        }

        /// <summary>
        /// 流上传
        /// </summary>
        /// <param name="file">上传的文件</param>
        /// <param name="folder">目标文件夹</param>
        /// <param name="filename"></param>
        /// <returns>指定文件名</returns>
        public static string ReadSteam(IFormFile file, string folder, string filename)
        {
            string fname = file.FileName;
            string lastname = Path.GetExtension(fname);
            string extname = $"{filename}{lastname}";
            string fullFileName = $"{folder}{extname}";

            ////创建文件夹，保存文件
            string? path = Path.GetDirectoryName(fullFileName);

            #region 检查上传的物理路径是否存在，不存在则创建
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path + "");
            }
            #endregion
            //保存文件  文件存在则先删除原来的文件
            if (System.IO.File.Exists(fullFileName))
            {
                System.IO.File.Delete(fullFileName);
            }
            //将流写入文件
            using Stream stream = file.OpenReadStream();
            // 把 Stream 转换成 byte[]
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            // 设置当前流的位置为流的开始
            stream.Seek(0, SeekOrigin.Begin);
            // 把 byte[] 写入文件
            FileStream fs = new(fullFileName, FileMode.Create);
            BinaryWriter bw = new(fs);
            bw.Write(bytes);
            bw.Close();
            fs.Close();
            return extname;
        }
    }
}
